//
//  UIButton_runtime.swift
//  boleh
//
//  Created by BO ZHOU on 2020/9/5.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import UIKit

private var btn_image_size = "keybtn_image_size"
private var button_click_block_key = "button_click_block_key"

public extension UIButton {
    
//    override open class func initRunTime() {
//        exchange(originalSelector: #selector(layoutSubviews), swizzledSelector: #selector(exchange_layoutSubviews))
//    }
    
    
    @objc override func exchange_layoutSubviews() {

        exchange_layoutSubviews()

        if let imageSize = self.image_size {

            let top_bottom = (self.height - imageSize.height)*0.5
            let left_right = (self.width - imageSize.width)*0.5

            self.imageEdgeInsets = UIEdgeInsets(top: top_bottom, left: left_right, bottom: top_bottom, right: left_right)
        }
        
//        DispatchQueue.main.async { [unowned self] in
//            if let imageSize = self.image_size {
//
//                let top_bottom = (self.height - imageSize.height)*0.5
//                let left_right = (self.width - imageSize.width)*0.5
//
//                self.imageEdgeInsets = UIEdgeInsets(top: top_bottom, left: left_right, bottom: top_bottom, right: left_right)
//            }
//        }

    }
    
    /**
     * 添加color_key, 格式 "#123456_0.40"
     */
    @IBInspectable var diy_titleColor: String {
        get {
            return ""
        }
        set {
            
            self.setTitleColor(colorWith(xibString: newValue) ??  UIColor.white, for: .normal)
        }
    }
    
    
    /** 修改imageview的大小 (防止image像素过大, 把imageview撑开), 配合交换layoutSubviews */
    var image_size: CGSize? {
        get {
            if let a = objc_getAssociatedObject(self, &btn_image_size) as? CGSize {
                return a
            }
            return nil
        }
        set {
            
            // zwarning: OBJC_ASSOCIATION_ASSIGN 使用这个坑逼, 不能正确存储
            objc_setAssociatedObject(self, &btn_image_size, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            

//            DispatchQueue.main.async { [unowned self] in
//                if let imageSize = self.image_size {
//
//                    let top_bottom = (self.height - imageSize.height)*0.5
//                    let left_right = (self.width - imageSize.width)*0.5
//
//                    self.imageEdgeInsets = UIEdgeInsets(top: top_bottom, left: left_right, bottom: top_bottom, right: left_right)
//                }
//            }
        }
    }
    
    
    /** 以block形式添加selector */
    var clickBlock: ((UIButton)->Void)? {
        get {
            if let a = objc_getAssociatedObject(self, &button_click_block_key) as? ((UIButton)->Void) {
                return a
            }
            return nil
        }
        set {
            
            // zwarning: OBJC_ASSOCIATION_ASSIGN 使用这个坑逼, 不能正确存储
            objc_setAssociatedObject(self, &button_click_block_key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            
            self.addTarget(self, action: #selector(btnClick(button:)), for: .touchUpInside)
        }
    }
    
    
    // todo: 回头测试一下这个button能不能被释放, 通过block回传self
    // 猜测不影响, 因为self是通过block参数回传, 并不是block内部代码捕获的变量
    @objc func btnClick(button: UIButton) {
        
        unowned let btn = self
        clickBlock?(btn)
    }
    
}
